
package com.ifcreate.infowell.common.exception;

import com.ifcreate.infowell.common.util.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常处理器
 * @author chenzedeng
 */
@RestControllerAdvice
public class ExceptionHandlerAdvice {
    private Logger logger = LoggerFactory.getLogger(getClass());


    @ExceptionHandler({CustomException.class})
    public JsonResult handleCustomException(CustomException e) {
        logger.error("服务器抛出自定义异常", e);
        return JsonResult.error(e.getMessage(), e.getErrorCode());
    }


    @ExceptionHandler(NoHandlerFoundException.class)
    public JsonResult handlerNoFoundException(Exception e) {
        logger.error(e.getMessage(), e);
        return JsonResult.error("路径不存在，请检查路径是否正确", 404);
    }



    @ExceptionHandler(Exception.class)
    public JsonResult handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return JsonResult.error("未知异常,请联系管理员");
    }
}
